import store, { AppDispatch } from '@/store/index';
import {
  addMyChannel,
  deleteChannelAPI,
  getChannelList,
  getMyChannelList,
} from '@/api/channel';
import { getChannel, hasToken, setChannel } from '@/utils/storage';
import { Channel } from '../reducer/channel';
import { Toast } from 'antd-mobile';

// 获取我的频道
export function myChannelListActionCreator() {
  return async (dispatch: AppDispatch) => {
    //   判断是否登录，登录了就发送请求，获取用户关注的频道。
    if (hasToken()) {
      const res = await getMyChannelList();
      // console.log(8, res);
      dispatch({
        type: 'channel/myChannelList',
        payload: res.data.channels,
      });
    } else {
      // 如果用户没有登录，获取本地存储频道。
      // 1. 本地存储有关注频道，直接使用。
      const localList = getChannel();
      if (localList.length) {
        dispatch({
          type: 'channel/myChannelList',
          payload: localList,
        });
      } else {
        // 2. 本地存储没有关注频道，发送请求获取一次。
        const res = await getChannelList();
        dispatch({
          type: 'channel/myChannelList',
          payload: res.data.channels,
        });
        //   再把获取到的数据存本地
        setChannel(res.data.channels);
      }
    }
  };
}

// 获取所有频道列表
export function getChannelActionCreator() {
  return async (dispatch: AppDispatch) => {
    const res = await getChannelList();
    // console.log(43, res);
    dispatch({
      type: 'channel/saveList',
      payload: res.data.channels,
    });
  };
}

// 添加频道去我的频道列表
// export function addMyChannelListActionCreator(payload: Channel) {
//   return {
//     type: 'channel/addChannelList',
//     payload,
//   };
// }

// 添加频道去我的频道列表(发请求版)
export function addMyChannelListActionCreator(item: Channel) {
  return async (dispatch: AppDispatch) => {
    const { userChannels } = store.getState().channel;
    // console.log(65, userChannels);

    const channels = [...userChannels, item];
    // console.log(68, channels);

    if (hasToken()) {
      // 发请求
      const res = await addMyChannel({ channels });
      // console.log(69, res);
    } else {
      // 用户未登录，把数据存储到本地
      setChannel(channels);
    }
    dispatch({
      type: 'channel/myChannelList',
      payload: channels,
    });
  };
}

// 修改点击高亮
export function changeMyChannelListActionCreator(payload: number) {
  return {
    type: 'channel/changeChannelactiveId',
    payload,
  };
}

// 删除频道
// export function deleteChannelActionCreator(payload: number) {
//   return {
//     type: 'channel/deleteChannel',
//     payload,
//   };
// }

// 删除频道数据（通过请求删除）
export function deleteChannelActionCreator(id: number) {
  return async (dispatch: AppDispatch) => {
    const { userChannels } = store.getState().channel;
    const newList = userChannels.filter((item) => item.id !== id);
    console.log(111, newList);
    if (newList.length < 5) return Toast.show('至少保留5个');
    if (id === 0) return Toast.show('推荐不能删除');
    if (hasToken()) {
      await deleteChannelAPI(id);
    } else {
      // 没登陆，就存储到本地
      setChannel(newList);
    }
    dispatch({
      type: 'channel/myChannelList',
      payload: newList,
    });
  };
}
